﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;

using Ordinaire.Db;
using Ordinaire.Security;

namespace Ordinaire.Test
{
    class Program
    {
        [STAThread]
        static void Main(string[] args)
        {
            // Setup test database
            SetupDb();

            string[] my_args = { Assembly.GetExecutingAssembly().Location, "/wait" };

            int returnCode = NUnit.ConsoleRunner.Runner.Main(my_args);

            if (returnCode != 0)
                Console.Beep();
        }

        /// <summary>
        /// Setup database for testing.
        /// </summary>
        static void SetupDb()
        {
            OContext context = DbFactory.Instance.CreateDataContext<OContext>();
            if (context.DatabaseExists())
            {
                context.DeleteDatabase();
            }
            context.CreateDatabase();

            string username = "admin";
            string password = "password";
            string roleName = "Administrator";

            MembershipManager membershipManager = new MembershipManager();
            membershipManager.CreateUser(username, password);

            RoleManager roleManager = new RoleManager();
            roleManager.CreateRole(roleName);
            roleManager.AddUsersToRoles(new string[] { username }, new string[] { roleName });
        }
    }
}
